<template>
  <div class="addressList">
    <header>
      <single-record type="backBox" name="通讯录" @click.native="backLast"></single-record>
      <p class="desc">{{ currentPart }}</p>
    </header>
    <div class="list self_scroll" @scroll="addressScroll">
      <div class="partAlpha" v-for="(item, index) in addressList" :key="item.code">
        <div class="desc" v-if="index !== 0">{{ item.code }}</div>
        <single-record
          @selectUser="selectUser"
          v-for="it in item.data"
          :key="it.user_id"
          :name="it.remarks ? it.remarks : it.user_name"
          :id="it.user_id"
          :imgID="it.user_img"
          :ref="'user' + it.user_id"
          :type="'checkBox'"
        ></single-record>
        <div class="gap"></div>
      </div>
    </div>
  </div>
</template>

<script>
import singleRecord from './singleRecord'
export default {
  components: {
    singleRecord
  },
  data () {
    return {
      partAlphaTopList: [], // 用来记载每个字母的位置
      currentPart: 'A'
    }
  },
  mounted () {
    this.getAddressList()
  },
  computed: {
    addressList () {
      const addressList = this.$store.getters.addressList
      return addressList
    }
  },
  methods: {
    getAddressList () {
      // 重置通讯录(A-Z)
      this.$store.dispatch('addressList/getMyAddressList')
    },
    addressScroll (e) {
      // 通讯录滚动
      const childrenList = [...e.target.children]
      this.partAlphaTopList = childrenList.map((item, index) => {
        const code =
          index === 0 ? this.addressList[0].code : item.children[0].innerText
        return { code, top: item.offsetTop - e.target.offsetTop }
      })
      const boxScrollTop = e.target.scrollTop
      let currentPart = null
      for (let i = 0; i < this.partAlphaTopList.length; i++) {
        if (i === 0) {
          continue
        }
        if (
          boxScrollTop >= this.partAlphaTopList[i - 1].top &&
          boxScrollTop <= this.partAlphaTopList[i + 1].top
        ) {
          currentPart = this.partAlphaTopList[i - 1]
        }
      }
      this.currentPart = currentPart.code
    },
    selectUser (user, val) { // 选择成员
      this.$emit('selectUser', user, val)
    },
    // 返回最近联系人
    backLast () {
      this.$emit('backRecent')
    }
  }
}
</script>

<style lang="scss" scoped>
.addressList {
  height: 100%;
  header {
    .desc {
      position: relative;
      padding: 9px 0px 4px 16px;
      opacity: 1;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      text-align: left;
      border-top: 1px solid #ebedf3;
      color: #435eea;
      line-height: 17px;
      // border-bottom: 1px solid #ebedf3;
      &::after {
        content: "";
        position: absolute;
        width: 260px;
        height: 1px;
        background-color: #ebedf3;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
  .list {
    height: calc(100% - 87px);
    .partAlpha {
      .desc {
        padding: 8px 0px 4px 16px;
        opacity: 1;
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: left;
        color: #9eaac4;
        line-height: 17px;
        position: relative;
        &::after {
          content: "";
          position: absolute;
          width: 260px;
          height: 1px;
          bottom: 0;
          left: 50%;
          background-color: #ebedf3;
          transform: translateX(-50%);
        }
      }
      .gap {
        width: 100%;
        height: 8px;
        background-color: #f6f7fb;
      }
    }
  }
}
</style>
